//
//  vdParticleEmitter.m
//  Void Dead
//
//  Created by Sidney Just on 21.11.09.
//
//  Copyright © 2009 by Sidney Just
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//  and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
//  PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//  FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
//  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "vdParticleEmitter.h"


@implementation vdParticleEmitter
@synthesize maxParticles, spawn, bright, gainSize, spawnPerFrame, rotSpeed;
@synthesize gain_X, gain_Y, randomX, randomY, gravityX, gravityY, lifespan, globalLifespan;
@synthesize pFlags;

#pragma mark -
#pragma mark Particles


- (void)draw:(NSTimeInterval)deltaTime {
	if(globalLifespan > -1)
	{
		globalLifespan -= father.deltaTime;
		if(globalLifespan <= 0)
		{
			stopSpawn = YES;
		}
	}
	
	if(!stopSpawn)
	{
		for(int i=0; i<spawnPerFrame; i++)
		{
			if(particleCount < maxParticles && texture != NULL && spawn == YES)
			{
				int i = particleCount;
				particleCount += 1;

				particles = (vdParticle **)realloc(particles, particleCount*sizeof(vdParticle *));
				particles[i] = (vdParticle *)malloc(sizeof(vdParticle));
				particles[i]->lifespan = lifespan;
				particles[i]->pos_x = 0 - (texture.dimension_x / 2);
				particles[i]->pos_y = 0 - (texture.dimension_y / 2);
				
				particles[i]->gain_X = gain_X;
				particles[i]->gain_Y = gain_Y;
				
				particles[i]->currForceX = 0.0f;
				particles[i]->currForceY = 0.0f;
				
				if(pFlags & ROTATION)
				{
					particles[i]->rotation = arc4random()%360;
				} else {
					particles[i]->rotation = 0;
				}
				
				particles[i]->shape = [container addShape:CGPointMake(particles[i]->pos_x, particles[i]->pos_y)];
				
				if(randomX > 0)
				{
					particles[i]->tPos_x = (float)(rand()%(int)randomX)-(int)(randomX/2);
				} else {
					particles[i]->tPos_x = 0;
				}
				
				if(randomY > 0)
				{
					particles[i]->tPos_y = (float)(rand()%(int)randomY)-(int)(randomY/2);
				} else {
					particles[i]->tPos_y = 0;
				}

				
			} else {
				break;
			}
		}
	}
	
	//float deltaTime = mother.deltaTime;
	
	container.color->red = color->red;
	container.color->green = color->green;
	container.color->blue = color->blue;
	container.color->alpha = color->alpha;
	
	BOOL removed = YES;
	while(removed) {
		removed = NO;
		for(int i=0; i<particleCount; i++)
		{
			particles[i]->lifespan -= deltaTime;
			
			particles[i]->currForceX += gravityX * deltaTime;
			particles[i]->currForceY += gravityY * deltaTime;
				
			particles[i]->shape->pos_x += (particles[i]->tPos_x + particles[i]->gain_X + particles[i]->currForceX) * deltaTime;
			particles[i]->shape->pos_y += (particles[i]->tPos_y + particles[i]->gain_Y + particles[i]->currForceY) * deltaTime;
			
			//scale += (gainSize * deltaTime);
			
			if(pFlags & ROTATE)
				particles[i]->rotation += rotSpeed*deltaTime;
			
			particles[i]->shape->rotation = particles[i]->rotation;
			
			//particles[i]->shape->pos_x = particles[i]->pos_x;
			//particles[i]->shape->pos_y = particles[i]->pos_y;
			
			if(particles[i]->lifespan <= 0)
			{
				[container removeShape:particles[i]->shape];
				free(particles[i]);
				
				for(int j=i; j<particleCount-1;j++)
				{
					particles[j] = particles[j+1];
				}
				
				particleCount -= 1;
				particles = (vdParticle **)realloc(particles, particleCount*sizeof(vdParticle *));
				
				removed = YES;
				break;
			}
		}
	}
	
	if(stopSpawn && container.shapeCount == 0 && parent)
	{
		[parent removeChild:self cleanUp:YES];
	}
}

+ (vdParticleEmitter *)emitter {
	return [[vdParticleEmitter alloc] init];
}

- (void)setTexture:(vdTexNode *)tex {
	[container setTexture:tex];
	texture = tex;
}

- (void)setFlags:(unsigned int)_flg {
	flags = _flg;
	container.flags = flags;
}

- (id)init {
	if(self	 = [super init])
	{
		particleCount = 0;
		particles = (vdParticle **)malloc(sizeof(vdParticle *));
		
		maxParticles = 0;
		texture = NULL;
		
		lifespan = 10;
		spawn = NO;
		
		gain_X = 0;
		gain_Y = 0;
		
		randomX = 0;
		randomY = 0;
		
		layer = 0;
		
		gravityX = 0;
		gravityY = 0;
		
		spawnPerFrame = 1;
		
		gainSize = 0;
		
		bright = NO;
		
		rotSpeed = 1;
		
		globalLifespan = -1;
		
		stopSpawn = NO;
		
		pFlags = 0;
		
		container = [[vdShapeContainer alloc] init];
		[self addChild:container];
		[container setAppendSize:500];
		
		srand(time(0));
	}
	return self;
}

- (void)dealloc {
	[self removeChild:container cleanUp:YES];
	[container release];
	
	for(int i=0; i<particleCount; i++)
		free(particles[i]);
	
	free(particles);
	
	[super dealloc];
}

@end
